import { Component, Injector, Inject, Optional, OpaqueToken, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';
import {
  API_BASE_URL,
  PagedResultDtoOfOrderForAdminOutDto,
  OrderDeliverInputDto,
  OrderServiceProxy
} from '@shared/service-proxies/service-proxies';
import { AppConsts } from '@shared/AppConsts';
import { OrderDetailComponent } from '../order-detail/order-detail.component';
@Component({
  selector: 'order-list',
  templateUrl: 'order-list.component.html',
  styleUrls: ['order-list.component.scss'],
  animations: [appModuleAnimation()]
})
export class OrderListComponent extends AppComponentBase implements OnInit {
  @ViewChild('orderDetailModal') orderDetailModal: OrderDetailComponent;
  orderList: PagedResultDtoOfOrderForAdminOutDto = new PagedResultDtoOfOrderForAdminOutDto();
  condition: GetOrdersCondition = new GetOrdersCondition();
  downloadUrl: string = AppConsts.remoteServiceBaseUrl + '/file/download/';
  currentPage: number = 0;
  constructor(
    injector: Injector,
    private _orderService: OrderServiceProxy,
    private route: ActivatedRoute
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.route.params.map(params => params['type']).subscribe((type) => {
      this.getOrders();
    });
  }

  getOrders(): void {

    this.condition.maxResultCount = 10;
    this.condition.skipCount = 0;
    this._orderService.getOrdersForAdminAsync(this.condition.isPayed,
      this.condition.userKeyword,
      this.condition.sorting,
      this.condition.keyword,
      this.condition.startTime,
      this.condition.endTime,
      this.condition.skipCount,
      this.condition.maxResultCount
    )
      .subscribe((result: PagedResultDtoOfOrderForAdminOutDto) => {
        this.orderList = result;
      });
  }

  orderDeliver(id: number): void {
    abp.message.confirm('确定物品已经发出?', '请确认', (result) => {
      if (result) {
        const orderDeliverInfo = new OrderDeliverInputDto();
        orderDeliverInfo.orderId = id;
        orderDeliverInfo.deliverDate = moment().format('YYYY-MM-DD');
        orderDeliverInfo.deliverTime = moment().format('HH:mm');
        this._orderService.deliverOrderAsync(orderDeliverInfo).subscribe(() => {
          abp.notify.success('操作成功!');
        });
      }
    });
  }

  pageChanged($event: any): void {
    this.condition.skipCount = this.condition.maxResultCount * $event.page;
    this.getOrders();
  }

  showDetail(id: number): void {
    this.orderDetailModal.show(id);
  }
}


export class GetOrdersCondition {
  isPayed?: boolean;
  userKeyword?: string;
  sorting?: string;
  keyword?: string;
  startTime?: moment.Moment;
  endTime?: moment.Moment;
  skipCount?: number;
  maxResultCount?: number;
}
